// $Id$

using System;
using System.Collections.Generic;
using System.Diagnostics;
using NUnit.Framework;
using Salamanca.DataAccess.Collections;
using Salamanca.DataActivities;
using Salamanca.DataActivities.UI;
using Northwind.DomainModel;
using Northwind.Activities;
using Northwind.Activities.OrderCreateNew;

namespace Salamanca.NUnit.DataActivities.Northwind
{
    internal sealed class OrderCreateNewQuestionFactory:
        IQuestionFactory
    {
        public OrderCreateNewQuestionFactory(Customer customer, DataMapperCollection dataMappers)
        {
            Debug.Assert(dataMappers!=null);
            if (dataMappers==null)
                throw new ArgumentNullException("dataMappers");

            _DataMappers=dataMappers;
            _Customer=customer;
        }

        public Question AskCustomerInfos(ActivityController controller, Customer domainModel)
        {
            domainModel=_Customer;

            return new AnsweredQuestion<Customer>(domainModel);
        }

        public Question AskOrderInfos(ActivityController controller, Order domainModel)
        {
            domainModel.OrderDate=DateTime.Today;

            return new AnsweredQuestion();
        }

        private DataMapperCollection _DataMappers;
        private Customer _Customer;
    }

    [TestFixture]
    public class OrderCreateNew:
        NorthwindTestBase
    {
        [Test]
        public void CanCreateNewOrder()
        {
            Customer customer=Customer.Find("FOLIG", DataMappers);

            ActivityController controller=new ActivityController(
                new OrderCreateNewActivity(
                    new Parameters(
                        new OrderCreateNewQuestionFactory(customer, DataMappers),
                        DataMappers
                    )
                )
            );
            controller.Execute();

            Assert.IsTrue(controller.HasCompleted);

            List<Order> orders=new List<Order>(Order.FindByCustomer(customer, DataMappers));
            Assert.AreEqual(6, orders.Count);

            Order order=orders.Find(
                delegate(Order o) {
                    return o.OrderDate==DateTime.Today;
                }
            );
            Assert.IsNotNull(order);
        }
    }

}
